package question.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import pay.activity.Pay;
import utils.KeyBoard;
import utils.ToastUtils;

/**
 * Created by Administrator on 2017/4/9.
 * 提问界面
 */

@ContentView(R.layout.ask)
public class Ask extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.ask_back)
    private ImageView ask_back;
    @ViewInject(R.id.ask_cancel)
    private TextView ask_cancel;
    @ViewInject(R.id.ask_content)
    private EditText ask_content;
    @ViewInject(R.id.ask_number)
    private TextView ask_number;
    @ViewInject(R.id.ask_commit)
    private Button ask_commit;

    //vip界面
    @ViewInject(R.id.vip_layout)
    private LinearLayout vip_layout;
    @ViewInject(R.id.ask_private_ask)
    private Button ask_private_ask;//私密提问
    @ViewInject(R.id.ask_public_ask)
    private Button ask_public_ask;//公开提问

    private String id;
    private String type;
    private String content;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    JSONObject jsonObject;
                    try {
                        jsonObject = new JSONObject((String) msg.obj);
                        String err_code = jsonObject.getString("err_code");
                        if ("0".equals(err_code)) {
                            //成功
                            ToastUtils.shortToast("问题发表成功");
                            Ask.this.finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");
        initView();
        KeyBoard.showKeyBoard();
    }

    private void initView() {
        content = "";
        ask_back.setOnClickListener(this);
        ask_cancel.setOnClickListener(this);
        switch (type) {
            case Constant.VOLUNTEER_TYPE_OFFICIAL:
                ask_commit.setVisibility(View.VISIBLE);
                vip_layout.setVisibility(View.GONE);
                ask_commit.setOnClickListener(this);
                break;
            case Constant.VOLUNTEER_TYPE_VIP:
                vip_layout.setVisibility(View.VISIBLE);
                ask_commit.setVisibility(View.GONE);
                ask_private_ask.setOnClickListener(this);
                ask_public_ask.setOnClickListener(this);
                break;
            case Constant.VOLUNTEER_TYPE_NORMAL:
            default:
                ask_commit.setVisibility(View.VISIBLE);
                vip_layout.setVisibility(View.GONE);
                ask_commit.setOnClickListener(this);
                break;
        }
        ask_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 255) {
                    ask_content.setText(content);
                    ask_content.setSelection(content.length());
                    ToastUtils.shortToast("输入字数已达上限");
                } else {
                    content = s.toString();
                }
                ask_number.setText("" + content.length());
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent i = new Intent();
        switch (v.getId()) {
            case R.id.ask_back:
            case R.id.ask_cancel:
                this.finish();
                break;
            case R.id.ask_commit:
                if (!TextUtils.isEmpty(content)) {
                    CommitQuestion(content);
                } else {
                    ToastUtils.shortToast("问题内容不能为空");
                }
                break;
            case R.id.ask_private_ask:
                //1元
                if (!TextUtils.isEmpty(content)) {
                    i.setClass(this, Pay.class);
                    i.putExtra("content", content);
                    i.putExtra("answerer_id", id);
                    i.putExtra("price", Constant.QUESTIONPRIVATE_PRICE);
                    i.putExtra("payType", Constant.QUESTIONPRIVATE);
                    startActivityForResult(i, 0);
                } else {
                    ToastUtils.shortToast("问题内容不能为空");
                }
                break;
            case R.id.ask_public_ask:
                //10元
                if (!TextUtils.isEmpty(content)) {
                    i.setClass(this, Pay.class);
                    i.putExtra("content", content);
                    i.putExtra("answerer_id", id);
                    i.putExtra("price", Constant.QUESTIONPUBLIC_PRICE);
                    i.putExtra("payType", Constant.QUESTIONPUBLIC);
                    startActivityForResult(i, 0);
                } else {
                    ToastUtils.shortToast("问题内容不能为空");
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_CANCELED) {
            if (resultCode == Constant.PAY_SUCCESS) {
                Ask.this.finish();
            }
        }
    }

    private void CommitQuestion(String content) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("content", content);
        hashMap.put("type", type);
        hashMap.put("answerer_id", id);
        new InterNetController(this, Constant.ASK, handler, hashMap, 0);
    }
}
